home *** CD-ROM | disk | FTP | other *** search
- /* All rights reserved. Copyright Ron Avitzur - 1992
- */
-
- #include "Recognizer.h"
-
- extern List patterns;
-
- void WriteData(FILE *file);
- void WriteGesturePattern(FILE *file,GesturePattern *gp);
- void WriteStrokePattern(FILE *file,StrokePattern *sp);
- void ReadData(FILE *file);
- void ReadGesturePattern(FILE *file,GesturePattern *gp);
- void ReadStrokePattern(FILE *file,StrokePattern *sp);
- void ConstructStr(Handle h,List s,short bits,char *name);
- void InterpretStr(char *str,List *s,short bits,char *name);
-
- short GetToken(char *token,char **sp);
- short GetToken(char *token,char **sp)
- {
- short i = 0;
- while (*sp && **sp == ' ') (*sp)++;
- while (*sp && **sp != ' ' && **sp != '\n') {
- token[i++] = **sp;
- (*sp)++;
- }
- token[i] = 0;
- return i != 0;
- }
-
- void InterpretStr(char *str,List *s,short bits,char *name)
- {
- char token[30];
- unsigned long n;
- short i;
-
- if (name) for (i = 0; i < strlen(str); i++) {
- if (str[i] == name[0]) str[i] = '0';
- else if (str[i] == name[1]) str[i] = '1';
- else if (str[i] == name[2]) str[i] = '2';
- else if (str[i] == name[3]) str[i] = '3';
- }
- while (GetToken(token,&str)) {
- ConvertStringToLong(token,&n,bits);
- Append(s,n);
- }
- }
-
- void ConstructStr(h,s,bits,name)
- Handle h;
- List s;
- short bits;
- char *name;
- {
- char str[255];
- short i,j,len;
-
- HUnlock(h);
- SetHandleSize(h,0);
- if (s == NIL) return;
- for (i = 0; i < s->num_items; i++) {
- ConvertLongToString(str,(long)s->items[i],bits,name);
- len = GetHandleSize(h);
- SetHandleSize(h,len + strlen(str) + 1);
- for (j = len; j < len + strlen(str); j++)
- (*h)[j] = str[j - len];
- (*h)[j] = ' ';
- }
- (*h)[j] = 0;
- HLock(h);
- }
-
- void ReadPatterns()
- {
- FILE *file;
- file = fopen("rxa.dat","r");
- ReadData(file);
- fclose(file);
- MakeSearchTables();
- }
-
- void SavePatterns()
- {
- FILE *file;
- file = fopen("rxa.dat","w");
- WriteData(file);
- fclose(file);
- }
-
- void ReadData(file)
- FILE *file;
- {
- short i,num_patterns;
- fscanf(file,"%d Patterns\n\n",&num_patterns);
- if (num_patterns == 0) {
- Append(&patterns,0L);
- patterns->num_items = 0;
- }
- for (i = 0; i < num_patterns; i++)
- {
- GesturePattern *gp = malloc(sizeof(GesturePattern));
- if (gp == NIL) {SysBeep(1); Debugger(); ExitToShell();}
- gp->strokes = NIL;
- ReadGesturePattern(file,gp);
- Append(&patterns,(long)gp);
- }
- }
-
- void WriteData(file)
- FILE *file;
- {
- short i;
- fprintf(file,"%d Patterns\n\n",patterns->num_items);
- for (i = 0; i < patterns->num_items; i++)
- WriteGesturePattern(file,patterns->items[i]);
- }
-
- void ReadGesturePattern(FILE *file,GesturePattern *gp)
- {
- short i,j,num_strokes; char c; char str[255];
- fscanf(file,"%s\n",str);
- if (strlen(str) == 1)
- gp->code = str[0];
- else sscanf(str,"%x",&gp->code);
-
- fscanf(file," %d\n",&num_strokes);
- for (i = 0; i < num_strokes; i++)
- {
- StrokePattern *sp = malloc(sizeof(StrokePattern));
- if (sp == NIL) {SysBeep(1); Debugger(); ExitToShell();}
- sp->is_dot = 0;
- for (j=0;j<NUM_FEATURES;j++) sp->s[j] = NIL;
- ReadStrokePattern(file,sp);
- Append(&gp->strokes,(long)sp);
- }
- fscanf(file,"\n");
- }
-
- void WriteGesturePattern(FILE *file,GesturePattern *gp)
- {
- short i;
-
- if (gp->code <= 0x7E && gp->code >= 0x21) fprintf(file, "%c\n",gp->code);
- else if (gp->code <= 0xf) fprintf(file,"0%x\n",gp->code);
- else fprintf(file, "%x\n",gp->code);
-
- fprintf(file," %d\n",gp->strokes->num_items);
- for (i = 0; i < gp->strokes->num_items; i++)
- WriteStrokePattern(file,gp->strokes->items[i]);
- fprintf(file,"\n");
- }
-
- void ReadStrokePattern(FILE *file,StrokePattern *sp)
- {
- char *p,type,str[1000],i;
-
- while (TRUE) {
- fgets(str,1000,file);
- if (str[0] == '\n') return;
- if (str[0] == '\t') { type = str[1]; p = &str[3]; }
- else { type = str[0]; p = &str[2]; }
- if (type == 'a') sp->is_dot = 1;
- else if ((i = type - 'b') < NUM_FEATURES)
- InterpretStr(p,&sp->s[i],Bits[i],Names[i]);
- }
- }
-
-
- void WriteStrokePattern(FILE *file,StrokePattern *sp)
- {
- short i;
- Handle h = NewHandle(0);
- if (sp->is_dot) fprintf(file,"\ta\n");
- for (i=0;i<NUM_FEATURES;i++) if (sp->s[i]) {
- ConstructStr(h,sp->s[i],Bits[i],Names[i]);
- fprintf(file,"\t%c: %s\n",(char)('b' + i),*h);
- }
- fprintf(file,"\n");
- DisposHandle(h);
- }
-
-